//
//  SSCatagoryViewController.swift
//  美团
//
//  Created by 叶竞泽 on 2017/5/15.
//  Copyright © 2017年 叶竞泽. All rights reserved.
//

import UIKit

let firstTableViewCellIdentifier = "firstTableViewCellIdentifier"
let secondTableViewCellIdentifier = "secondTableViewCellIdentifier"

protocol SSCatagoryViewControllerDelegate: NSObjectProtocol {
    func didSeleted(withTitle: String, iconName: String, hlIconName: String, subName: String?)
}

class SSCatagoryViewController: UIViewController {
    
    // MARK: - 属性
    
    @IBOutlet weak var firstTableView: UITableView!
    @IBOutlet weak var secondTableView: UITableView!
    
    weak var delegate: SSCatagoryViewControllerDelegate?
    
    /// 左边的tableView的数据
    fileprivate lazy var firstTableViewData: [[String: Any]]? = {
        guard let plistPath = Bundle.main.path(forResource: "categories.plist", ofType: nil) else {
            return nil
        }
        let url = URL(fileURLWithPath: plistPath)
        let array = NSArray(contentsOf: url)
        return array as? [[String: Any]]
    }()
    /// 右边的tableView的数据
    fileprivate var secondTableViewData: [String]? {
        didSet {
            if secondTableViewData == nil {
                secondTableView.isHidden = true
            } else {
                secondTableView.reloadData()
                secondTableView.isHidden = false
            }
            
        }
    }
    /// 当前选中的cell索引
    fileprivate var currentIndexOfCell: Int = 0 {
        didSet {
            secondTableViewData = firstTableViewData?[currentIndexOfCell]["subcategories"] as? [String]
        }
    }
    
    // MARK: - 生命周期
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        setupTableView()
    }
}

// MARK: - 设置界面
extension SSCatagoryViewController {
    fileprivate func setupUI() {
        view.backgroundColor = UIColor.white
    }
    fileprivate func setupTableView() {
        firstTableView.register(UITableViewCell.self, forCellReuseIdentifier: firstTableViewCellIdentifier)
        secondTableView.register(UITableViewCell.self, forCellReuseIdentifier: secondTableViewCellIdentifier)
        secondTableView.isHidden = true
    }
}

// MARK: - 实现数据源方法和代理方法
extension SSCatagoryViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == firstTableView {
            print(tableView)
            return firstTableViewData?.count ?? 10
        } else {
            return secondTableViewData?.count ?? 0
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell: UITableViewCell?
        if tableView == firstTableView {
            cell = tableView.dequeueReusableCell(withIdentifier: firstTableViewCellIdentifier, for: indexPath)
            cell?.selectionStyle = .gray
            cell?.textLabel?.text = firstTableViewData?[indexPath.row]["name"] as? String
            if let iconName = firstTableViewData?[indexPath.row]["icon"] as? String {
                cell?.imageView?.image = UIImage(named: iconName)
            }
            if firstTableViewData?[indexPath.row]["subcategories"] != nil {
                cell?.accessoryView = UIImageView(image: UIImage(named: "icon_filter_arrow"))
            } else {
                cell?.accessoryType = .none
            }
        }
        if tableView == secondTableView {
            cell = tableView.dequeueReusableCell(withIdentifier: secondTableViewCellIdentifier, for: indexPath)
            if secondTableViewData != nil {
                cell?.textLabel?.text = secondTableViewData?[indexPath.row]
            }
        }
        cell?.textLabel?.font = UIFont.systemFont(ofSize: 11)
        cell?.textLabel?.adjustsFontSizeToFitWidth = true
        cell?.textLabel?.textColor = UIColor.darkGray
        
        return cell!
    }
    
    /// 选择菜单点击后的效果 - 使控制器消失,把选中的信息传给home控制器,让topView改变图标
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        var name: String?
        var icon: String?
        var highlightIcon: String?
        var subName: String?
        if tableView == firstTableView {
            currentIndexOfCell = indexPath.row
            if firstTableViewData?[indexPath.row]["subcategories"] == nil {
                name = firstTableViewData?[indexPath.row]["name"] as? String
                icon = firstTableViewData?[indexPath.row]["small_icon"] as? String
                highlightIcon = firstTableViewData?[indexPath.row]["small_highlighted_icon"] as? String
                subName = nil
                dismiss(animated: true, completion: {
                })
                delegate?.didSeleted(withTitle: name!, iconName: icon!, hlIconName: highlightIcon!, subName: subName)
            }
        }
        if tableView == secondTableView {
            icon = firstTableViewData?[currentIndexOfCell]["small_icon"] as? String
            name = firstTableViewData?[currentIndexOfCell]["name"] as? String
            if let subTitles = firstTableViewData?[currentIndexOfCell]["subcategories"] as? [String] {
                subName = subTitles[indexPath.row]
            } else {
                subName = nil
            }
            highlightIcon = firstTableViewData?[currentIndexOfCell]["small_highlighted_icon"] as? String
            dismiss(animated: true, completion: {
            })
            delegate?.didSeleted(withTitle: name!, iconName: icon!, hlIconName: highlightIcon!, subName: subName)
        }
    }

}
